//
//  DDGoodsListToolBar.m
//  Dingdong
//
//  Created by 程强 on 16/11/25.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDGoodsListToolBar.h"

@interface DDGoodsListToolBar()
@property (strong, nonatomic) UIImageView *whole_arrow;
@property (strong, nonatomic) UIImageView *priceUpArrow;
@property (strong, nonatomic) UIImageView *priceDropArrow;
@property (strong, nonatomic) UIButton *lastBtn;
@property (assign, nonatomic) NSInteger touchPriceCount;
@end

@implementation DDGoodsListToolBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        UIButton *wholeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:wholeBtn];
        [wholeBtn setTitle:@"Whole" forState:UIControlStateNormal];
        [wholeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [wholeBtn setTitleColor:kRedColor forState:UIControlStateSelected];
        wholeBtn.titleLabel.font =[UIFont systemFontOfSize:15.0];
        wholeBtn.tag = 100;
        wholeBtn.selected = YES;
        self.lastBtn = wholeBtn;
        [wholeBtn addTarget:self action:@selector(toolBarSortClick:) forControlEvents:UIControlEventTouchUpInside];
        [wholeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(self).offset(15);
            make.width.equalTo(@60);
            make.height.equalTo(@30);
        }];
        
        self.whole_arrow = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        _whole_arrow.image = LoadImage(@"goods_red_arrow");
        [self addSubview:_whole_arrow];
        [_whole_arrow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(wholeBtn.mas_right).offset(2);
            make.width.equalTo(@10);
            make.height.equalTo(@6);
        }];
        
        UIButton *volumeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [volumeBtn setTitle:@"Sales volume" forState:UIControlStateNormal];
        [volumeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [volumeBtn setTitleColor:kRedColor forState:UIControlStateSelected];
        volumeBtn.titleLabel.font =[UIFont systemFontOfSize:15.0];
        [self addSubview:volumeBtn];
        [volumeBtn addTarget:self action:@selector(toolBarSortClick:) forControlEvents:UIControlEventTouchUpInside];
        volumeBtn.tag = 101;
        [volumeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.centerX.equalTo(self);
            make.height.equalTo(@30);
            make.width.equalTo(@100);
        }];
        
        UIButton *priceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [priceBtn setTitle:@"Price" forState:UIControlStateNormal];
        [priceBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [priceBtn setTitleColor:kRedColor forState:UIControlStateSelected];
        priceBtn.titleLabel.font =[UIFont systemFontOfSize:15.0];
        [self addSubview:priceBtn];
        [priceBtn addTarget:self action:@selector(toolBarSortClick:) forControlEvents:UIControlEventTouchUpInside];
        priceBtn.tag = 102;
        [priceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.width.equalTo(@60);
            make.height.equalTo(@30);
            make.right.equalTo(self).offset(-25);
        }];
        self.touchPriceCount = 0;
        
        self.priceUpArrow = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        _priceUpArrow.image = LoadImage(@"goods_price_up");
        _priceUpArrow.highlightedImage = LoadImage(@"goods_red_up_arrow");
        [self addSubview:_priceUpArrow];
        [_priceUpArrow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@7);
            make.height.equalTo(@5);
            make.centerY.equalTo(self).offset(-3);
            make.left.equalTo(priceBtn.mas_right);
        }];
        
        self.priceDropArrow = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        _priceDropArrow.image = LoadImage(@"goods_price_drop");
        _priceDropArrow.highlightedImage = LoadImage(@"goods_red_arrow");
        [self addSubview:_priceDropArrow];
        [_priceDropArrow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@7);
            make.height.equalTo(@5);
            make.centerY.equalTo(self).offset(3);
            make.left.equalTo(priceBtn.mas_right);
        }];
    }
    return self;
}
- (void)toolBarSortClick:(UIButton *)btn
{
    if (self.lastBtn!=btn) {
        self.lastBtn.selected = NO;
    }
    btn.selected = YES;
    DingDongSortGoodsType sortType = DingDongSortGoodsTypeWhole;
    switch (btn.tag) {
        case 100:
            sortType = DingDongSortGoodsTypeWhole;
            _priceDropArrow.highlighted = NO;
            _priceUpArrow.highlighted = NO;
            self.touchPriceCount = 0;
            break;
        case 101:
            sortType = DingDongSortGoodsTypeVolume;
            _priceDropArrow.highlighted = NO;
            _priceUpArrow.highlighted = NO;
            self.touchPriceCount = 0;
            break;
        case 102:
        {
            self.touchPriceCount+=1;
            if (self.touchPriceCount%2 == 0) {
                sortType = DingDongSortGoodsTypeLowPrice;
                _priceDropArrow.highlighted = YES;
                _priceUpArrow.highlighted = NO;
            }else{
                sortType = DingDongSortGoodsTypeHighPrice;
                _priceUpArrow.highlighted = YES;
                _priceDropArrow.highlighted = NO;
            }
        }
            break;
            
        default:
            break;
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(toolBar:didSelectIndex:)]) {
        [self.delegate toolBar:self didSelectIndex:sortType];
    }
    self.lastBtn = btn;
}
@end
